/**
 * 试卷相关API
 */
import { get, post, put, del } from './index'
import type {
  ApiResponse,
  PaginatedResponse,
  PaginationParams,
  ExamPaperTemplate,
  ExamPaper
} from '@/types'

// 试卷模板管理
export const getExamPaperTemplates = (params?: PaginationParams) => {
  return get<ApiResponse<PaginatedResponse<ExamPaperTemplate>>>('/api/v1/exam-papers/templates/', params)
}

export const getExamPaperTemplate = (id: number) => {
  return get<ApiResponse<ExamPaperTemplate>>(`/api/v1/exam-papers/templates/${id}/`)
}

export const createExamPaperTemplate = (data: Partial<ExamPaperTemplate>) => {
  return post<ApiResponse<ExamPaperTemplate>>('/api/v1/exam-papers/templates/', data)
}

export const updateExamPaperTemplate = (id: number, data: Partial<ExamPaperTemplate>) => {
  return put<ApiResponse<ExamPaperTemplate>>(`/api/v1/exam-papers/templates/${id}/`, data)
}

export const deleteExamPaperTemplate = (id: number) => {
  return del(`/api/v1/exam-papers/templates/${id}/`)
}

// 试卷管理
export const getExamPapers = (params?: PaginationParams) => {
  return get<ApiResponse<PaginatedResponse<ExamPaper>>>('/api/v1/exam-papers/', params)
}

export const getExamPaper = (id: number) => {
  return get<ApiResponse<ExamPaper>>(`/api/v1/exam-papers/${id}/`)
}

export const createExamPaper = (data: Partial<ExamPaper>) => {
  return post<ApiResponse<ExamPaper>>('/api/v1/exam-papers/', data)
}

export const updateExamPaper = (id: number, data: Partial<ExamPaper>) => {
  return put<ApiResponse<ExamPaper>>(`/api/v1/exam-papers/${id}/`, data)
}

export const deleteExamPaper = (id: number) => {
  return del(`/api/v1/exam-papers/${id}/`)
}

// 试卷预览
export const previewExamPaper = (id: number) => {
  return get<ApiResponse<{ paper: ExamPaper; questions: any[] }>>(`/api/v1/exam-papers/${id}/preview/`)
}

// 试卷发布
export const publishExamPaper = (id: number) => {
  return post<ApiResponse<ExamPaper>>(`/api/v1/exam-papers/${id}/publish/`)
}

// 试卷复制
export const copyExamPaper = (id: number, data: { title: string; description?: string }) => {
  return post<ApiResponse<ExamPaper>>(`/api/v1/exam-papers/${id}/copy/`, data)
}

// 智能组卷
export const smartPaperGeneration = (data: {
  title: string
  description?: string
  template_id?: number
  total_score: number
  passing_score: number
  duration: number
  question_config: {
    question_bank_id?: number
    question_types: Record<string, number>
    difficulty_distribution: Record<string, number>
    tags?: string[]
  }
}) => {
  return post<ApiResponse<ExamPaper>>('/api/v1/exam-papers/smart-generation/', data)
}

// 试卷导出
export const exportExamPaper = (id: number, format: 'pdf' | 'word' | 'excel') => {
  return get(`/api/v1/exam-papers/${id}/export/?format=${format}`, {}, {
    responseType: 'blob'
  })
}

// 试卷统计
export const getExamPaperStatistics = () => {
  return get<ApiResponse<{
    total_papers: number
    published_papers: number
    draft_papers: number
    recent_papers: ExamPaper[]
  }>>('/api/v1/exam-papers/statistics/')
}

// 导出统一的试卷API对象
export const examPaperApi = {
  // 模板相关
  getExamPaperTemplates,
  getExamPaperTemplate,
  createExamPaperTemplate,
  updateExamPaperTemplate,
  deleteExamPaperTemplate,

  // 试卷相关
  getExamPapers,
  getExamPaper,
  createExamPaper,
  updateExamPaper,
  deleteExamPaper,

  // 功能接口
  previewExamPaper,
  publishExamPaper,
  copyExamPaper,
  smartPaperGeneration,
  exportExamPaper,
  getExamPaperStatistics
}
